package com.awphone.listener;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.apache.log4j.Logger;

import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

/**
 * @author a_polyakov
 */
@WebListener
public class ServletContextListenerImpl implements ServletContextListener {
    private static final Logger LOG = Logger.getLogger(ServletContextListenerImpl.class);

    private static Gson gson;
    private static EntityManagerFactory entityManagerFactory;

    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {
        LOG.debug("Application start");

        final GsonBuilder gsonbuilder = new GsonBuilder();
        gson = gsonbuilder.create();

        entityManagerFactory = Persistence.createEntityManagerFactory("AWPhone");
        //TODO
    }

    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {
        //TODO
        LOG.debug("Application stop");
    }

    public static Gson getGson() {
        return gson;
    }

    public static EntityManagerFactory getEntityManagerFactory() {
        return entityManagerFactory;
    }
}
